package com.cskaoyan.controller;

import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.AddressVo;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.vo.IdDeletedAddress;
import com.cskaoyan.domain.MarketAddress;
import com.cskaoyan.service.AddressService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("wx/address")
public class WxAddressController {
    @Autowired
    AddressService addressService;
    @GetMapping("list")
    public BaseRespVo list(){
        PageHelper.startPage(1,6);
        List<MarketAddress> addressList = addressService.selectAllAddress();
        CommonData data = CommonData.data(addressList);
        return BaseRespVo.ok(data);
    }
    @PostMapping("save")
    public BaseRespVo save(@RequestBody AddressVo addressVo){
        String tel = addressVo.getTel();
        MarketAddress address = addressService.selectOneAddressByTel(tel);
        if (address != null){
            addressService.updateAddressById(addressVo);
        }else{
            addressService.insertAddress(addressVo);
        }
        int id = 0;
        if (address != null && address.getId() != null) {
            id = address.getId();
        }
        return BaseRespVo.ok(id);
    }
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody IdDeletedAddress deletedId){
        int userId = deletedId.getId();
        addressService.deleteAddressById(userId);
        return BaseRespVo.ok();
    }
    @GetMapping("detail")
    public BaseRespVo detail(Integer id){
        MarketAddress address = addressService.selectAddressById(id);
        return BaseRespVo.ok(address);
    }
}
